//app.js
import util from "./utils/util";

App({
    onLaunch: function () {
        // 展示本地存储能力
        var logs = wx.getStorageSync('logs') || []
        logs.unshift(Date.now())
        wx.setStorageSync('logs', logs)
        let h = wx.getSystemInfoSync().windowHeight
        let w = wx.getSystemInfoSync().windowWidth

        this.globalData.screenH = (h * (750 / w))
        console.log(this.globalData.screenH)
        let _this = this
        // 登录
        wx.login({
            success: res => {
                // 发送 res.code 到后台换取 openId, sessionKey, unionId
                _this.globalData.code = res.code
                console.log('code:' + res.code)
                wx.request({
                    url: util.baseUrl + '/wx/user/' + util.appid + '/login',
                    data: {
                        code: res.code
                    },
                    header: {
                        "Content-Type": "application/x-www-form-urlencoded"
                    },
                    method: 'GET',
                    success: res => {
                        console.log('RES: ' + JSON.stringify(res))
                        _this.globalData.sessionKey = res.data.sessionKey
                        _this.globalData.openid = res.data.openid

                        console.log('获取到了openid:' + res.data.openid)
                        if (_this.sessionKeyReadCallBack) {
                            _this.sessionKeyReadCallBack(res)
                        }
                    }
                });
            }
        })
        // 获取用户信息
        wx.getSetting({
            success: res => {
                if (res.authSetting['scope.userInfo']) {
                    // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                    wx.getUserInfo({
                        success: res => {
                            // 可以将 res 发送给后台解码出 unionId
                            this.globalData.userInfo = res.userInfo

                            this.postUserInfo(res,this)
                            // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
                            // 所以此处加入 callback 以防止这种情况
                            if (this.userInfoReadyCallback) {
                                this.userInfoReadyCallback(res)
                            }
                        }
                    })
                }
            }
        })
    },
    postUserInfo(res,that) {
        console.log('userinfo->app.js:' + JSON.stringify(res))
        wx.request({
            url: util.baseUrl + '/wx/user/' + util.appid + '/info',
            method: "POST",
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            data: {
                sessionKey: that.globalData.sessionKey,
                signature: res.signature,
                rawData: res.rawData,
                encryptedData: res.encryptedData,
                iv: res.iv
            },
            success: res => {
                console.log("请求成功")
            }
        })
    },

    globalData: {
        userInfo: null,
        screenH: null,
        screenW: null,
        openid: null,
        code: null
    }
})